package com.example.im.ui.adapter

import android.content.Context
import android.graphics.Color
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.LinearLayout
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.bumptech.glide.Glide
import com.example.im.R
import com.example.im.bean.ChatRecord
import com.example.im.common.Constants
import com.example.im.ui.custom.VoiceTextView
import com.xlkj.ehobe.storage.ui.adapter.holder.RecyclerViewHolder
import kotlinx.android.synthetic.main.linear_item_chat_file.view.*
import kotlinx.android.synthetic.main.linear_item_chat_photo.view.*
import kotlinx.android.synthetic.main.linear_item_chat_text.view.*
import kotlinx.android.synthetic.main.linear_item_chat_title.view.*
import kotlinx.android.synthetic.main.linear_item_chat_video.view.*
import kotlinx.android.synthetic.main.linear_item_chat_voice.view.*
import kotlinx.android.synthetic.main.recycler_item_chat_other_3.view.*
import kotlinx.android.synthetic.main.recycler_item_chat_receive_2.view.*
import kotlinx.android.synthetic.main.recycler_item_chat_send_2.view.*
import java.text.DecimalFormat

class ChatAdapter2(
    private val mContext: Context,
    private val mChatRecords: MutableList<ChatRecord>
) :
    RecyclerView.Adapter<RecyclerViewHolder>() {

    private var mOnClickItemListener: OnClickItemListener? = null

    fun setOnClickItemListener(mOnClickItemListener: OnClickItemListener) {
        this.mOnClickItemListener = mOnClickItemListener
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerViewHolder {

        val chatRecord = this.mChatRecords[viewType]

        val view = LayoutInflater.from(mContext).inflate(
            when (chatRecord.type) {
                Constants.CHAT_TYPE_TEXT -> R.layout.linear_item_chat_text
                Constants.CHAT_TYPE_VOICE -> R.layout.linear_item_chat_voice
                Constants.CHAT_TYPE_PHOTO -> R.layout.linear_item_chat_photo
                Constants.CHAT_TYPE_VIDEO -> R.layout.linear_item_chat_video
                Constants.CHAT_TYPE_FILE -> R.layout.linear_item_chat_file
                else -> R.layout.linear_item_chat_text
            }, parent, false
        )

        return when {
            chatRecord.or == Constants.CHAT_ACTION_1 -> {
                val inflate = LayoutInflater.from(mContext)
                    .inflate(R.layout.recycler_item_chat_send_2, parent, false)
                inflate.linear_chat_send_holder.addView(view)
                RecyclerViewHolder(inflate)
            }
            chatRecord.or == Constants.CHAT_ACTION_2 -> {
                if (chatRecord.type == Constants.CHAT_TYPE_CHANGE) {
                    val inflate = LayoutInflater.from(mContext)
                        .inflate(R.layout.recycler_item_chat_other_3, parent, false)
                    RecyclerViewHolder(inflate)
                } else {

                    val inflate = LayoutInflater.from(mContext)
                        .inflate(R.layout.recycler_item_chat_receive_2, parent, false)
                    if (chatRecord.kind == Constants.CHAT_KIND_2) {
                        val titleView = LayoutInflater.from(mContext).inflate(
                            R.layout.linear_item_chat_title, parent, false
                        )
                        titleView.tv_chat_item_title_name.text = chatRecord.from!!.substring(
                            chatRecord.from!!.indexOf("/") + 1, chatRecord.from!!.length
                        )
                        inflate.linear_chat_receive_holder.addView(titleView)
                    }
                    inflate.linear_chat_receive_holder.addView(view)
                    RecyclerViewHolder(inflate)
                }
            }
            else -> {
                val inflate = LayoutInflater.from(mContext)
                    .inflate(R.layout.recycler_item_chat_other_3, parent, false)
                RecyclerViewHolder(inflate)
            }
        }
    }

    override fun onBindViewHolder(holder: RecyclerViewHolder, position: Int) {


        val view: View = holder.itemView

        val chatRecord = this.mChatRecords[position]


        when (chatRecord.type) {
            Constants.CHAT_TYPE_TEXT -> {
                this.setChangeTextView(view.tv_chat_item_text, chatRecord.or)
                view.tv_chat_item_text.text = chatRecord.body
                view.tv_chat_item_text.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentLong(v, position)
                    return@setOnLongClickListener true
                }
            }
            Constants.CHAT_TYPE_VOICE -> {
                this.setChangeTextView(view.tv_chat_item_voice, chatRecord.or)
                val duration = chatRecord.file!!.duration
                val durationStr = if (duration > 9999) {
                    "${duration.toString().substring(0, 2)} \""
                } else {
                    "${duration.toString().substring(0, 1)} \""
                }
                val linearParams = view.tv_chat_item_voice.layoutParams as LinearLayout.LayoutParams
                if (duration > 9999) {
                    linearParams.width = 520
                } else {
                    linearParams.width = 320
                }
                view.tv_chat_item_voice.layoutParams = linearParams
                view.tv_chat_item_voice.text = durationStr
                view.tv_chat_item_voice.setOnClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentVoice(v, position)
                }
                view.tv_chat_item_voice.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentLong(v, position)
                    return@setOnLongClickListener true
                }
                if (view.tv_chat_item_voice.isPlaying) {
                    view.tv_chat_item_voice.startAnim()
                } else {
                    view.tv_chat_item_voice.stopAnim()
                }
                view.tv_chat_item_voice.tag = position
            }
            Constants.CHAT_TYPE_PHOTO -> {
                val url = chatRecord.file!!.netUrl ?: chatRecord.file!!.localUrl
                Glide.with(mContext).load(url).into(view.img_chat_item_photo)
                view.img_chat_item_photo.setOnClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentPhoto(v, position)
                }
                view.img_chat_item_photo.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentLong(v, position)
                    return@setOnLongClickListener true
                }
            }
            Constants.CHAT_TYPE_VIDEO -> {
                val url = chatRecord.file!!.netUrl ?: chatRecord.file!!.localUrl
                Glide.with(mContext).load(chatRecord.file!!.localUrl).into(view.img_chat_item_video)
//                view.tv_chat_item_video_time.text = chatRecord.file!!.duration.toString()
                view.ll_chat_item_video.setOnClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentVideo(v, position)
                }
                view.ll_chat_item_video.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentLong(v, position)
                    return@setOnLongClickListener true
                }
            }
            Constants.CHAT_TYPE_FILE -> {
                view.tv_chat_item_file_name.text = chatRecord.file!!.name
                val fileLength = byteskb(chatRecord.file!!.length.toLong())
                view.tv_chat_item_file_length.text = fileLength
                view.cl_chat_item_files.setOnClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentFiles(v, position)
                }
                view.cl_chat_item_files.setOnLongClickListener { v ->
                    mOnClickItemListener!!.onClickItemContentLong(v, position)
                    return@setOnLongClickListener true
                }
            }
            else -> {
                view.tv_other_info.text = chatRecord.body
            }
        }

        if (chatRecord.or == Constants.CHAT_ACTION_1) {
            when (chatRecord.status) {
                Constants.CHAT_STATUS_2 -> {
                    //发送中
                    view.progress_chat_send_load.visibility = View.VISIBLE
                    view.img_chat_send_fail.visibility = View.GONE
                }
                Constants.CHAT_STATUS_3 -> {
                    //发送失败
                    view.progress_chat_send_load.visibility = View.GONE
                    view.img_chat_send_fail.visibility = View.VISIBLE
                    view.img_chat_send_fail.setOnClickListener { v ->
                        mOnClickItemListener!!.onClickItemChatRelay(v, position)
                    }
                }
                else -> {
                    //发送成功
                    view.img_chat_send_fail.visibility = View.GONE
                    view.progress_chat_send_load.visibility = View.GONE
                }
            }

            view.img_chat_send_heard.setOnClickListener { v ->
                mOnClickItemListener!!.onClickItemChatHeard(v, position)
            }
        } else if (chatRecord.or == Constants.CHAT_ACTION_2) {
            if (chatRecord.type == Constants.CHAT_TYPE_CHANGE) {

            } else {
                view.img_chat_receive_heard.setOnClickListener { v ->
                    mOnClickItemListener!!.onClickItemChatHeard(v, position)
                }
                if (chatRecord.kind == Constants.CHAT_KIND_2) {
                    view.img_chat_receive_heard.setOnLongClickListener { v ->
                        mOnClickItemListener!!.onClickItemChatLong(v, position)
                        return@setOnLongClickListener true
                    }
                } else {
                    view.img_chat_receive_heard.setOnLongClickListener(null)
                }
            }
        } else {

        }

        view.tag = position

    }

    override fun getItemCount(): Int {
        return mChatRecords.size
    }

    override fun getItemViewType(position: Int): Int {
        return position
    }

    private fun getItemPosition(id: Int): Int {
        for (index in mChatRecords.size - 1 downTo 0)
            if (mChatRecords[index].id == id)
                return index
        return -1
    }

    fun addDataItem(entire: ChatRecord) {
        if (this.mChatRecords.none { it.id == entire.id }) {
            this.mChatRecords.add(itemCount, entire)
            this.notifyItemChanged(itemCount)
        } else {
            val position = getItemPosition(entire.id)
            if (position == -1)
                return
            this.mChatRecords[position] = entire
            this.notifyItemChanged(position)
        }
    }


    fun removeDataItem(index: Int) {
        this.mChatRecords.removeAt(index)
        this.notifyDataSetChanged()
//        this.notifyItemRemoved(index)
//        this.notifyItemRangeRemoved(index, itemCount)
    }

    fun clearData() {
        this.mChatRecords.clear()
        this.notifyDataSetChanged()
    }

    interface OnClickItemListener {

        fun onClickItemChatHeard(v: View, position: Int)
        fun onClickItemChatRelay(v: View, position: Int)
        fun onClickItemChatLong(v: View, position: Int)

        fun onClickItemContentVoice(v: View, position: Int)
        fun onClickItemContentPhoto(v: View, position: Int)
        fun onClickItemContentVideo(v: View, position: Int)
        fun onClickItemContentFiles(v: View, position: Int)

        fun onClickItemContentLong(v: View, position: Int)
    }

    private fun setChangeTextView(view: TextView, or: Int) {
        if (or == Constants.CHAT_ACTION_1) {
            view.setTextColor(Color.WHITE)
            view.setBackgroundResource(R.drawable.style_chat_content_text)
            view.setLinkTextColor(mContext.resources.getColor(R.color.white))
            if (view is VoiceTextView) {
                view.setDrawableRight(R.drawable.animation_voice_right)
            }
        } else if (or == Constants.CHAT_ACTION_2) {
            view.setTextColor(Color.BLACK)
            view.setBackgroundResource(R.drawable.style_chat_content_text_receive)
            view.setLinkTextColor(mContext.resources.getColor(R.color.blue))
            if (view is VoiceTextView) {
                view.setDrawableLeft(R.drawable.animation_voice_left)
            }
        }
    }

    private fun byteskb(bytes: Long): String {
        val GB = 1024 * 1024 * 1024
        val MB = 1024 * 1024
        val KB = 1024
        val format = DecimalFormat("###.0")
        return when {
            bytes / GB >= 1 -> "${format.format(bytes / GB)}GB"
            bytes / MB >= 1 -> "${format.format(bytes / MB)}MB"
            bytes / KB >= 1 -> "${format.format(bytes / KB)}KB"
            else -> "${bytes}B"
        }
    }
}
